package cn.iocoder.yudao.module.reading.controller.app.community;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.reading.service.community.CommunityUploadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 用户 APP - 社区文件上传 Controller
 *
 * @author 芋道源码
 */
@Tag(name = "用户 APP - 社区文件上传")
@RestController
@RequestMapping("/reading/community/upload")
@Validated
@Slf4j
public class CommunityUploadController {

    @Resource
    private CommunityUploadService uploadService;

    @PostMapping("/image")
    @Operation(summary = "上传帖子图片")
    @PreAuthenticated
    public CommonResult<String> uploadPostImage(
            @Parameter(description = "图片文件", required = true) @RequestParam("file") MultipartFile file) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        String imageUrl = uploadService.uploadPostImage(file, userId);
        return success(imageUrl);
    }

}
